//
//  DBMainController.swift
//  Weibo
//
//  Created by zyc on 16/6/3.
//  Copyright © 2016年 heima. All rights reserved.
//

import UIKit

//swift文件中，对于不需要公开的属性和方法，我们需要加一个private
//面试题： 在开发中你是怎么调试bug的？
//1. 用断点调式
//2. 看调用堆栈
//3. 暴力测试， 用NSLog和注释部分代码的方法调试bug
//4. 直接google或百度

//去墙： shadowsock

//tabBar的子view是在viewWillAppear才创建，定制tabBar时，需要将定制的子view移到最上层： bringSubviewToFront

class DBMainController: UITabBarController {

    private lazy var composeButton: UIButton = UIButton(title: "",  image: "tabbar_compose_icon_add", backGroundImage: "tabbar_compose_button", target: self, action: "compose:", event: .TouchUpInside)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.whiteColor()

        addChildViewControllers()
        
        addComposeButton()
        
        settabBar()
        
    }
    
    override func viewWillAppear(animated: Bool) {
        
        super.viewWillAppear(animated)
        
        tabBar.bringSubviewToFront(composeButton)
        
    }

}

extension DBMainController {

    /**
    添加子控制器
    
    - parameter controller 子控制器
    - parameter title title
    - parameter iconImageName tabBarItem的图片名字
    */
    private func addChildViewController(controller: UIViewController, title: String, iconImageName: String?) {
        
        let navigationHome = UINavigationController(rootViewController: controller);
        
        controller.title = title
        
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.grayColor()], forState: .Normal)
        
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
        
        if let iconImageName = iconImageName {
        
            let selectedImageName = "\(iconImageName)_selected"
            
            controller.tabBarItem.image = UIImage(named: iconImageName)?.imageWithRenderingMode(.AlwaysOriginal)
            
            controller.tabBarItem.selectedImage = UIImage(named: selectedImageName)?.imageWithRenderingMode(.AlwaysOriginal)
        
        }
        
        addChildViewController(navigationHome)
        
    }
    
    /**
    *  添加所有的子控制器
    */
    private func addChildViewControllers() {
        
        //添加主页
        let home = DBHomeViewController()
        addChildViewController(home, title: "首页", iconImageName: "tabbar_home")
        
        //消息页面
        let message = DBMessageViewController()
        addChildViewController(message, title: "消息", iconImageName: "tabbar_message_center")
        
        //占位控制器
        let viewController = UIViewController()
        addChildViewController(viewController, title: "", iconImageName: nil)
        
        //发现页面
        let discover = DBDiscoverViewController()
        addChildViewController(discover, title: "发现", iconImageName: "tabbar_discover")
        
        //我页面
        let profile = DBProfileViewController()
        addChildViewController(profile, title: "我", iconImageName: "tabbar_profile")
        
    }
    
    private func addComposeButton () {
        
        self.tabBar.addSubview(composeButton)
        
        //根据一个frame，向内或向外生成一个新的frame
        let tabBarItemWidth = tabBar.bounds.size.width/5
        
        let composeFrame = CGRectInset(tabBar.bounds,tabBarItemWidth*2, 0)
        
        composeButton.frame = composeFrame
        
    }
    
    //设置tabbar
    private func settabBar() {
        
        //tabBar.tintColor = UIColor.orangeColor()
        //设置tabbar的阴影和背影颜色
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
        
        tabBar.shadowImage = UIImage.createAdotImage(UIColor(white: 0.9, alpha: 1.0))
        
    }
    
    func compose(button: UIButton) {
        
        print("发布");
        
    }

}


